package com.surprise.db.datasources;

import java.util.Map;

import javax.sql.DataSource;

import lombok.Data;

import org.springframework.jdbc.core.JdbcTemplate;

import com.google.common.collect.Maps;

/**
 *
 * @author nick
 * 2013-11-17
 */
@Data
public class JdbcTemplateWrapper {
    public Map<String, JdbcTemplate> jdbcTemplates = Maps.newHashMap();

    public JdbcTemplateWrapper(DataSourceWrapper wrapper) {
        Map<String, DataSource> map = wrapper.dataSources;
        for (Map.Entry<String, DataSource> entry : map.entrySet()) {
            jdbcTemplates.put(entry.getKey(),
                    new JdbcTemplate(entry.getValue()));
        }
    }

    public JdbcTemplate getJdbcTemplateByDataSourceName(String dataSourceName) {
        if (jdbcTemplates.containsKey(dataSourceName)) {
            return jdbcTemplates.get(dataSourceName);
        } else {
            throw new RuntimeException(dataSourceName + " not exists");
        }
    }
}
